package com.micro.school.edu.service.impl;

import java.util.List;

import com.micro.school.common.core.context.SecurityContextHolder;
import com.micro.school.common.core.utils.DateUtils;
import com.micro.school.common.core.web.domain.AjaxResult;
import com.micro.school.common.security.utils.SecurityUtils;
import com.micro.school.edu.convert.SchoolGradeConvert;
import com.micro.school.edu.domain.SchoolGrade;
import com.micro.school.edu.domain.vo.SchoolFeeYearVo;
import com.micro.school.edu.domain.vo.SchoolGradeVo;
import com.micro.school.edu.mapper.SchoolGradeMapper;
import com.micro.school.edu.service.ISchoolGradeService;
import com.micro.school.system.api.RemoteOrderService;
import com.micro.school.system.api.RemoteUserService;
import com.micro.school.system.api.domain.vo.SchoolGradeVos;
import com.micro.school.system.api.domain.vo.StudentInfoVos;
import com.micro.school.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;


/**
 * 学校年级Service业务层处理
 *
 * @author rakic
 * @date 2025-08-07
 */
@Service
public class SchoolGradeServiceImpl implements ISchoolGradeService
{
    @Autowired
    private SchoolGradeMapper schoolGradeMapper;

    @Autowired
    private RemoteUserService remoteUserService;

    @Autowired
    private RemoteOrderService remoteOrderService;

    /**
     * 查询学校年级
     *
     * @param id 学校年级主键
     * @return 学校年级
     */
    @Override
    public SchoolGradeVo selectSchoolGradeById(Long id)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        return schoolGradeMapper.selectSchoolGradeById(id);
    }



    /**
     * 查询学校年级
     *
     * @param id 学校年级主键
     * @return 学校年级
     */
    @Override
    public SchoolGradeVos selectSchoolGradeByIds(Long id)
    {
        return schoolGradeMapper.selectSchoolGradeByIds(id,SecurityContextHolder.getUserTenantId());
    }

    @Override
    public List<SchoolGradeVos> selectSchoolGradeLists(SchoolGradeVos schoolGradeVos) {
        schoolGradeVos.setTenantId(Long.valueOf(SecurityContextHolder.getUserTenantId()));
        return schoolGradeMapper.selectSchoolGradeLists(schoolGradeVos);
    }

    /**
     * 查询学校年级列表
     *
     * @param schoolGradeVo 学校年级
     * @return 学校年级
     */
    @Override
    public List<SchoolGradeVo> selectSchoolGradeList(SchoolGradeVo schoolGradeVo)
    {
        schoolGradeVo.setTenantId(Long.valueOf(SecurityContextHolder.getUserTenantId()));
        return schoolGradeMapper.selectSchoolGradeList(schoolGradeVo);
    }

    /**
     * 新增学校年级
     *
     * @param schoolGradeVo 学校年级
     * @return 结果
     */
    @Override
    public AjaxResult insertSchoolGrade(SchoolGradeVo schoolGradeVo)
    {
        SchoolGrade schoolGrade = SchoolGradeConvert.INSTANCE.convert(schoolGradeVo);

        schoolGrade.setCreateTime(DateUtils.getNowDate());
        schoolGrade.setTenantId(Long.valueOf(SecurityContextHolder.getUserTenantId()));
        StudentInfoVos studentInfoVos = remoteUserService.selectSchoolById(Long.valueOf(SecurityContextHolder.getUserTenantId()));
        schoolGrade.setSchoolName(studentInfoVos.getSchoolName());
        SchoolGradeVos schoolGrade1 = new SchoolGradeVos();
        schoolGrade1.setGrade(schoolGradeVo.getGrade());
        schoolGrade1.setTenantId(Long.valueOf(SecurityContextHolder.getUserTenantId()));
        schoolGrade1.setDeleteFlag("0");
        List<SchoolGradeVos> schoolGradeVos = schoolGradeMapper.selectSchoolGradeLists(schoolGrade1);
        if (schoolGradeVos != null && schoolGradeVos.size() > 0){
            return AjaxResult.error("已有同名年级");
        }else{
            schoolGradeMapper.insertSchoolGrade(schoolGrade);
            return AjaxResult.success("成功插入");
        }

    }

    /**
     * 修改学校年级
     *
     * @param schoolGradeVo 学校年级
     * @return 结果
     */
    @Override
    public int updateSchoolGrade(SchoolGradeVo schoolGradeVo)
    {
        SchoolGrade schoolGrade = SchoolGradeConvert.INSTANCE.convert(schoolGradeVo);
        schoolGrade.setUpdateTime(DateUtils.getNowDate());
        SchoolGradeVo schoolGradeVo1 = schoolGradeMapper.selectSchoolGradeById(schoolGradeVo.getId());
        if (!schoolGradeVo.getGrade().equals(schoolGradeVo1.getGrade())){
            remoteUserService.updateAllSchoolGrade(schoolGradeVo.getGrade(),schoolGradeVo.getId());
        }
        return schoolGradeMapper.updateSchoolGrade(schoolGrade);
    }

    /**
     * 批量删除学校年级
     *
     * @param ids 需要删除的学校年级主键
     * @return 结果
     */
    @Override
    public int deleteSchoolGradeByIds(List<Long> ids)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        return schoolGradeMapper.deleteBatchIds(ids);
    }

    /**
     * 删除学校年级信息
     *
     * @param id 学校年级主键
     * @return 结果
     */
    @Override
    public int deleteSchoolGradeById(Long id)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        return schoolGradeMapper.deleteById(id);
    }

}
